package com.example.zqh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zqh.mapper.DeptMapper;
import com.example.zqh.model.RespBean;
import com.example.zqh.pojo.Dept;
import com.example.zqh.pojo.Emp;
import com.example.zqh.mapper.EmpMapper;
import com.example.zqh.pojo.params.EmpParams;
import com.example.zqh.pojo.params.OneStringOneNumber;
import com.example.zqh.service.EmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zqh.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-09-10
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    EmpMapper empMapper;

    @Autowired
    DeptMapper deptMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    /**
      * @Author: zqh
      * @Date:
      * @Description: 添加一个新员工
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean addANewEmployee(EmpParams empParams) {
       //判断员工电话号码是否重复
        Emp phone = empMapper.selectOne(new QueryWrapper<Emp>().eq("phone", empParams.getPhone()));
        AssertUtil.isTrue(phone != null,"该号码已存在");

        //判断员工名字是否重复
        Emp name = empMapper.selectOne(new QueryWrapper<Emp>().eq("name", empParams.getName()));
        AssertUtil.isTrue(name != null, "该员工名已经存在");

        //判断传递过来的部门名是否存在
        Dept dept = deptMapper.selectOne(new QueryWrapper<Dept>().eq("name", empParams.getDept_name()));
        AssertUtil.isTrue(dept == null,"没有这个部门存在");

        //获取当前时间
        Date date = new Date();
        Emp emp = new Emp();
        emp.setName(empParams.getName())
                .setPassword(empParams.getPassword())
                .setPhone(empParams.getPhone())
                //默认设置一个头像
                .setHeadUrl("http://60.205.253.99:9000/zqh/11de1a52-0c10-4b68-aaa4-97704efacc6c.jpg")
                .setJoinDate(date)
                //默认工资3000块
                .setSalary(3000.0)
                .setDeptId(dept.getId())
                .setDeptName(dept.getName())
                //是否未该部的部长，默认不是
                .setMonitor(0);

        int insert = empMapper.insert(emp);
        AssertUtil.isTrue(insert != 1,"注册成为员工失败");

        return RespBean.success("注册成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 员工信息的修改
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyEmployeeInformationInterface(Emp emp) {
        int update = empMapper.updateById(emp);
        AssertUtil.isTrue(update != 1,"用户信息修改失败");
        return RespBean.success("用户信息修改成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 员工修改自己姓名接口
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean employeeSOwnNameChangeInterface(Integer id,String name) {
        Emp emp1 = empMapper.selectById(id);
        AssertUtil.isTrue(emp1 == null,"参数传递错误");
        //先判断名字在员工表里是否存在，存在就不能更改
        Emp emp = empMapper.selectOne(new QueryWrapper<Emp>().eq("name", name));
        AssertUtil.isTrue(emp != null,"改名字已存在");

        emp1.setName(name);
        int i = empMapper.updateById(emp1);
        AssertUtil.isTrue(i != 1,"名字修改失败");
        return RespBean.success("名字修改成功");
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 员工修改自己的密码
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean employeesChangeTheirPasswords(OneStringOneNumber oneStringOneNumber) {
        Emp emp = empMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(emp == null,"参数传递错误");
        emp.setPassword(oneStringOneNumber.getText());
        int i = empMapper.updateById(emp);
        AssertUtil.isTrue(i != 1, "修改密码失败");
        return RespBean.success("密码修改成");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 员工修改电话接口
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean employeeModifiesPhoneInterface(OneStringOneNumber oneStringOneNumber) {
        //判断电话号码是否重复
        Emp phone = empMapper.selectOne(new QueryWrapper<Emp>().eq("phone", oneStringOneNumber.getText()));
        AssertUtil.isTrue(phone != null,"该电话号码以存在");

        Emp emp = empMapper.selectById(oneStringOneNumber.getId());
        emp.setPhone(oneStringOneNumber.getText());

        int i = empMapper.updateById(emp);
        AssertUtil.isTrue(i != 1,"员工修改电话号码失败");
        return RespBean.success("员工修改电话号码成功");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean employeeModificationAvatarInterface(OneStringOneNumber oneStringOneNumber) {
        Emp emp = empMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(emp == null,"参数传递错误");
        emp.setHeadUrl(oneStringOneNumber.getText());
        int i = empMapper.updateById(emp);
        AssertUtil.isTrue(i != 1, "员工修改头像失败");
        return RespBean.success("员工修改头像成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 获取所有员工的数量
    */
    @Transactional(readOnly = true)
    @Override
    public RespBean getAllEmpty() {
        Integer number = empMapper.getEmptyNumber();
        return RespBean.success("查询成功",number);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 分页查询员工
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean queryEmployeesByPage(Integer page, Integer size) {
        List<Emp> emps = empMapper.paginationToQueryAllEmployees((page - 1) * size, size);
        return RespBean.success("查询成功",emps);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 员工删除接口
    */
    @Override
    @Transactional
    public RespBean employeeDeleteInterface(Integer id) {
        int i = empMapper.deleteById(id);
        AssertUtil.isTrue(i != 1,"员工删除失败");
        return RespBean.success("员工删除成功");
    }
}
